package com.yishan.thread;


import lombok.Data;

/**
 * 未使用  ThreadLocal  线程之间的变量会不一致
 **/
@Data
public class ThreadLocalTest {

    private String data;

    public static void main(String[] args) {
        final ThreadLocalTest demo = new ThreadLocalTest();

        for (int i = 0; i < 5; i++) {

            Thread thread = new Thread(new Runnable() {
                public void run() {
                    demo.setData(Thread.currentThread().getName() + "的数据");
                    System.out.println("---------------------------------");
                    System.out.println(Thread.currentThread().getName() + "------->" + demo.getData());
                }
            });
            thread.setName("线程" + i);
            thread.start();

        }
        //运行结果
        //---------------------------------
        //---------------------------------
        //---------------------------------
        //线程0------->线程2的数据
        //线程1------->线程2的数据
        //---------------------------------
        //线程2------->线程3的数据
        //线程3------->线程3的数据
        //---------------------------------
        //线程4------->线程4的数据

    }

}
